package com.haoshuang.zhouzhoubang.ui.page.order.receiveorder

import android.graphics.Paint
import android.text.TextPaint
import android.view.View
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ItemReceiveOrderListBinding
import com.haoshuang.zhouzhoubang.model.bean.HomeBean
import com.haoshuang.zhouzhoubang.ui.dialog.ShowServiceItemDialog
import com.haoshuang.zhouzhoubang.ui.dialog.ShowSkillListDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.visibleOrGone

/**
 * 抢单任务适配器
 */
class ReceiveOrderListAdapter(data: MutableList<HomeBean>) : BaseBindingQuickAdapter<HomeBean, ItemReceiveOrderListBinding>(data) {

    override fun onBindViewHolder(holder: BaseBindingHolder, position: Int, item: HomeBean?) {
        if (item == null) return
        holder.getViewBinding<ItemReceiveOrderListBinding>().apply {
            ivPlatformOrder.visibleOrGone(item?.is_platform_order == 1)
            tvDistance.text=item?.distance+"km"
            val tp: TextPaint = tvOrderTitle.paint
            tp.strokeWidth = 1.0f
            tp.style = Paint.Style.FILL_AND_STROKE
            tvServiceTime.text=item?.service_time
            tvServiceAddress.text=item?.user_address
            tvRemark.text=item?.mark
            if (item.hourly_worker_time.isNullOrBlank()){
                tvOrderTitle.text="${item.store_name}"
            }else{
                if (item.hourly_worker_time=="0"){
                    tvOrderTitle.text="${item.store_name}"
                }else{
                    tvOrderTitle.text="${item.store_name} [${item.hourly_worker_time}小时]"
                }
            }

            tvPrice.text="¥"+item.pay_price
            if (item.show_status=="1"){
                ivOrderType.setImageResource(R.mipmap.taoc)
            }else if (item.show_status=="2"){
                ivOrderType.setImageResource(R.mipmap.tddd09)
            }else{
                ivOrderType.setImageResource(R.mipmap.grdd09)
            }
            if (item.show_status=="1"){
                ivOrderType1.setImageResource(R.mipmap.taoc)
            }else if (item.show_status=="2"){
                ivOrderType1.setImageResource(R.mipmap.tddd09)
            }else{
                ivOrderType1.setImageResource(R.mipmap.grdd09)
            }

            if (item.is_cart_order==1){
                layoutServiceNum.visibility = View.VISIBLE
                tvServiceNum.text ="共"+ item.order_goods.size+"个服务项"
                ivOrderType.visibility = View.GONE
                ivOrderType1.visibility = View.VISIBLE
            }else{
                layoutServiceNum.visibility = View.GONE
                ivOrderType.visibility = View.VISIBLE
                ivOrderType1.visibility = View.GONE
            }

            layoutServiceNum.click {
                val list = item?.order_goods
                if (list.isNullOrEmpty()){
                    return@click
                }
                XPopup.Builder(context)
                    .asCustom(ShowServiceItemDialog(context, list))
                    .show()
            }

            // 是否显示奖励金
            llRewardView.visibleOrGone(item.reward_amount != null && 0.0 != item.reward_amount.toDoubleOrNull())
            tvRewardMoney.text = "${item.reward_amount}元"

            // 预付款
            val prepayments_amount = item.prepayments_amount?.toDoubleOrNull()?:0.0
            ivAdvanceCharge.visibleOrGone(prepayments_amount != 0.0)

            tvTotalNum.text = "数量 x${item?.total_num.toString()}"

            tvKeyArea.visibleOrGone(!item.key_areas.isNullOrEmpty())
            tvKeyArea.text = "重点区域: ${item?.key_areas}"
        }
    }
}
